package com.industrial.premu.util;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

public class BandwidthMonitor {

    public static void main(String[] args) {
        OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        // 获取网络IO信息，不同的JVM实现可能返回不同的值
        // 对于Sun的JVM，可以转换为sun.management.OperatingSystemImpl类型来访问更多方法
        // 如getCommittedVirtualMemorySize()
        com.sun.management.OperatingSystemMXBean sunOsBean = (com.sun.management.OperatingSystemMXBean) osBean;

        // 获取总的上行带宽
        long totalUploadThroughput = sunOsBean.getCommittedVirtualMemorySize() / 1024; // 转换为KB

        // 获取总的下行带宽
        long totalDownloadThroughput = sunOsBean.getFreePhysicalMemorySize() / 1024; // 转换为KB

        System.out.println("Total Upload Throughput: " + totalUploadThroughput + " KB/s");
        System.out.println("Total Download Throughput: " + totalDownloadThroughput + " KB/s");
    }
}
